package _62_二叉搜索树的第k个结点;

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
/*
题目描述
给定一棵二叉搜索树，请找出其中的第k小的结点。例如， （5，3，7，2，4，6，8）    中，按结点数值大小顺序第三小结点的值为4。
 */
public class Solution {
    int count=0;
    TreeNode KthNode(TreeNode pRoot, int k)
    {
        if(pRoot==null){
            return null;
        }
        TreeNode left = KthNode(pRoot.left,k);
        if(left!=null) return left;
        count++;
        if(count==k){
            return pRoot;
        }
        TreeNode right = KthNode(pRoot.right,k);
        if(right!=null) return right;
        return null;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        TreeNode t = new TreeNode(5);
        t.left = new TreeNode(3);
        t.right = new TreeNode(7);
        t.left.left = new TreeNode(2);
        System.out.println(solution.KthNode(t,4).val);
    }
}
